
from rest_framework import serializers
from xj_users.models import City
from xj_users.models import Enterprise
from xj_users.models import Recruit

# 热门城区序列化器all
class CitySerializer(serializers.ModelSerializer):

    class Meta:
        model = City
        fields = '__all__'



# class RecruitSerializeraa(serializers.ModelSerializer):
#     class Meta:
#         model = Recruit
#         fields = ['id']



class EnterpriseSerializerSimple(serializers.ModelSerializer):
    # recruits = RecruitSerializeraa(read_only=True, many=True)

    class Meta:
        model = Enterprise

        # 在企业模型类中有个隐藏字段recruits，表示职位列表
        # 直接写入是id
        fields = ('id', 'name','labels','logo','recruits','summary')

# 职位里面嵌套企业的部分信息  以为在职位模型类中已经有enterprise字段，只要嵌套操作就行
class RecruitSerializerSimple(serializers.ModelSerializer):
    enterprise = EnterpriseSerializerSimple(read_only=True)

    class Meta:
        model = Recruit
        fields = ('id', 'jobname','salary','condition','education','type','city','createtime','enterprise','labels')


class EnterpriseSerializer(serializers.ModelSerializer):

    # 在企业模型类中有个隐藏的recruits字段， 表示职位列表，返回企业情况，嵌套职位情况使用
    recruits = RecruitSerializerSimple(many=True, read_only=True)

    class Meta:
        model = Enterprise
        fields = "__all__"



# 职位信息全部返回，用于返回职位详情
class RecruitSerializer(serializers.ModelSerializer):
    enterprise = EnterpriseSerializer(read_only=True)

    class Meta:
        model = Recruit
        fields = "__all__"


